/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.toools.gwt.wmc.client.company.gui;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Widget;
import java.util.Collection;
import java.util.Iterator;
import java.util.Vector;
import org.toools.gwt.widget.client.bc.BasicControl;
import org.toools.gwt.widget.client.bc.BasicListRenderer;
import org.toools.gwt.widget.client.bc.BasicTogglePanel;
import org.toools.gwt.widget.client.bc.DefaultCallback;
import org.toools.gwt.widget.client.bc.DefaultDeleteTogglePanel;
import org.toools.gwt.widget.client.bc.RelationControl;
import org.toools.gwt.widget.client.cloud.CloudListener;
import org.toools.gwt.widget.client.cloud.TagVO;

import org.toools.gwt.wmc.client.company.CompanyServiceAsync;
import org.toools.gwt.wmc.client.companypart.CompanyPartService;
import org.toools.gwt.wmc.client.companypart.gui.CompanyPartControl;
import org.toools.gwt.wmc.client.companypart.gui.CompanyPartPanel;
import org.toools.gwt.wmc.jpa.Company;
import org.toools.gwt.wmc.jpa.CompanyPart;

/**
 *
 * @author Manuel
 */
public class CompanyListRenderer extends BasicListRenderer{

    private Company c = null;
    private CompanyPartControl cpc = null;

    BasicTogglePanel companyparts = new BasicTogglePanel("set companyparts", 1, true) {
        public void onActive() {
            clear();
            addChild(new CompanyPartsRelation((Company) getTag().getAttachedBusinessObject()));
        }
    };

    org.toools.gwt.widget.client.bc.ToggleButton tb = new org.toools.gwt.widget.client.bc.ToggleButton("companyparts", false) {
        public void toggle() {
            super.toggle();
            if(cpc != null) {
                cpc.setVisible(isOn());
            }
        }
    };

    public CompanyListRenderer(TagVO tag, BasicControl bc) {
        super(tag,bc);
        c = (Company) tag.getAttachedBusinessObject();

        cpc = new CompanyPartControl(CompanyPartService.Util.getInstance(),c);
        cpc.setVisible(false);
        addLeft(tb);
        addControl(cpc);
        addButton(companyparts);
    }

    @Override
    public void setTag(TagVO tag) {
        Company comp = (Company) tag.getAttachedBusinessObject();
		setTitle(comp.getName());
    }

    public Widget getChildBasicPanel() {
        CompanyPartPanel cpp = (CompanyPartPanel) getBasicControl().getBasicPanel();
        cpp.setParentCompany((Company)getTag().getAttachedBusinessObject());
        return cpp;
    }
    public String getExpandMessage() {
        return "<div><img src='folder.png'/> " + c.getCompanyParts().size() + " childs</div>";
    }
    public boolean getExpandVisible() {
        return c.getCompanyParts().size() > 0;
    }

    public DefaultDeleteTogglePanel getDefaultDeleteTogglePanel() {
        return new DefaultDeleteTogglePanel(getBasicControl(), getTag().getAttachedBusinessObject()) {
            @Override
            public boolean valid() {
                Company c = (Company) getTag().getAttachedBusinessObject();
                if (c.getCompanyParts().size() > 0) {
                    return false;
                } else {
                    return true;
                }
            }
            public String getDeleteConfirmMessage() {
                if(!valid())
                    return "deletion not possible with childs.";
                else
                    return getBasicControl().getDeleteConfirmMessage();
            }
        };
    }

    public class CompanyPartsRelation extends RelationControl {
        Company c;
        public CompanyPartsRelation(Company c) {
            super(false, true, true, false);
            this.c = c;
            CompanyPartService.Util.getInstance().getCompanyParts(loadCallback);
            this.getRelationList().addCloudListener(updateCloudListener);
        }
        public TagVO getTagVO(Object bp) {
            CompanyPart cp = (CompanyPart) bp;
            TagVO result = new TagVO("" + cp.getId(), cp.getName(), 0, 0);
            result.setAttachedBusinessObject(cp);
            return result;
        }
        AsyncCallback loadCallback = new DefaultCallback() {
            public void onSuccess(Object result) {
                super.onSuccess(result);
                setTags(getTagVO(((Vector<CompanyPart>) result).toArray()));
                if((c.getCompanyParts() != null) && (c.getCompanyParts().size() > 0)) {
                    String[] active = new String[c.getCompanyParts().size()];
                    int j = 0;
                    for(Iterator i = c.getCompanyParts().iterator(); i.hasNext();) {
                        CompanyPart cp = (CompanyPart) i.next();
                        active[j++] = "" + cp.getId();
                    }
                    getRelationList().setActive(active);
                }
                getStatus().setText("");
            }
        };
        CloudListener updateCloudListener = new CloudListener() {
            public void onSelectionChanged(java.util.Iterator tags) {
                Collection<CompanyPart> cps = new Vector<CompanyPart>();
                for(; tags.hasNext();) {
                    TagVO tag = (TagVO) tags.next();
                    cps.add((CompanyPart)tag.getAttachedBusinessObject());
                }
                c.setCompanyParts(cps);
                ((CompanyServiceAsync)getBasicControl().getService()).update(c, new DefaultCallback());
            }
            public void onTagActivated(TagVO tag) {}
            public void onTagDeactivated(TagVO tag) {}
            public void onFilterChanged() {}
            public void onTagsChanged() {}
        };
    }
}
